package com.ga.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ga.model.Album;
import com.ga.model.Reply;
import com.ga.model.User;
import com.ga.model.Video;
import com.ga.service.UploadService;
import com.ga.util.Common;
import com.ga.util.MessageUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class VideoController extends Controller {
	@ActionKey("/video/list")
	public void list() {
		try {
			Map<String,Object> paraMap=new HashMap<String, Object>();
			paraMap.put("v.title",getPara("title"));
			paraMap.put("v.description",getPara("description"));
			paraMap.put("v.tag",getPara("tag"));
			paraMap.put("u.uname",getPara("uname"));
			paraMap.put("a.aname",getPara("aname"));
			paraMap.put("v.playamount>",getPara("playamount"));
			paraMap.put("v.praise>",getPara("praise"));
			paraMap.put("v.isenable=",1);
			paraMap.put("v.status=",getPara("status"));
			paraMap.put("v.createtime1",getPara("createtime1"));
			paraMap.put("v.createtime2",getPara("createtime2"));
			paraMap.put("v.lastmodifytime1",getPara("lastmodifytime1"));
			paraMap.put("v.lastmodifytime2",getPara("lastmodifytime2"));
			Map<String,Object> wheresMap=Common.wheres(paraMap);
			Object[] param=(Object[])wheresMap.get("param");
			int pn = Integer.parseInt(getPara("pageNumber")==null?"1":getPara("pageNumber"));
			int ps = Integer.parseInt(getPara("pageSize")==null?"10":getPara("pageSize"));	
			String select="select a.aname,u.uname,v.id,v.title,v.front_pic,v.praise,v.playamount,v.status,v.createtime,v.lastmodifytime,(select count(1) from reply r where r.video_id=v.id) replycount,"+Common.videocase();
			String sqlExceptSelect=" from video v left join album a on(v.album_id=a.id) left join users u on(v.users_id=u.id) where ";
			Page<Video> list=Video.dao.paginate(pn, ps, select, sqlExceptSelect+wheresMap.get("wheres"),param);
			renderJson(list);
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	@ActionKey("/video/add/save")
	public void addsave() {
		try {
			Video video=new Video();
			video.set("title", getPara("title"));
			video.set("description", getPara("description"));
			video.set("videopath", getPara("videopath"));
			video.set("tag", getPara("tag"));
			video.set("album_id", getPara("album_id"));
			video.set("front_pic", getPara("front_pic"));
			video.set("lastmodifytime", new Date());
			video.save();
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	
	@ActionKey("/video/frontpic")
	public void avatar(){
		  Map<String,String> m=new HashMap<String,String>();
		  try{
			int maxFileSize =1024*1024*1; //1M
			int maxMemSize = 1024*1024*1; //1M
			String filePath = this.getRequest().getRealPath("")+"\\resources\\video\\";
			String id=getPara("id");
			String contentType = this.getRequest().getContentType();
			if ((contentType.indexOf("multipart/form-data") >= 0)) {
				String fileName=UploadService.instance.upload(maxMemSize, maxFileSize, id, filePath, this.getRequest());
				m.put("poster", "resources/video/"+fileName);
			}
		} catch (Exception e) {
			m.put("poster", "");
			e.printStackTrace();
		}
		 renderJson(m);
	}
	
	@ActionKey("/video/detail")
	public void detail() {
		Map<String,Object> map=new HashMap<String,Object>();
		String id=getPara(0)==null?getPara("id"):getPara(0);
		String sql="select a.aname,u.uname,v.*,(select count(1) from reply r where r.video_id=v.id) replycount,"+Common.videocase()
				+ "from video v left join album a on(v.album_id=a.id) left join users u on(v.users_id=u.id) where v.id=?";
		try {
			Video video=Video.dao.findFirst(sql,id);
			map.put("flag", MessageUtil.SUCCESS.toString());
			map.put("video", video);
		} catch (Exception e) {
			map.put("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
		renderJson(map);
	}
	
	@ActionKey("/video/update/save")
	public void updsave() {
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			Video video=new Video();
			video.set("id", getPara("id"));
			video.set("title", getPara("title"));
			video.set("description", getPara("description"));
			video.set("front_pic", getPara("front_pic"));
			video.set("lastmodifytime", new Date());
			video.update();
			map.put("flag", MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			map.put("flag", MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
		renderJson(map);
	}
	@ActionKey("/video/ispass")
	public void pass() {
		try {
			Object[] param={getPara("status"),getPara("id")};
			Db.update("update video set status = ? where id = ?",param);
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	@ActionKey("/video/start")
	public void start() {
		try {
			Db.update("update video set playamount = playamount+1 where id = ?",getPara("id"));
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	@ActionKey("/video/praise")
	public void praise() {
		try {
			Db.update("update video set praise = praise+1 where id = ?",getPara("id"));
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	@ActionKey("/video/delete")
	public void delete() {
		String id=getPara(0)==null?getPara("id"):getPara(0);
		try {
			Db.update("update video set isenable = 0 where id = ? ",id);
			Db.update("update reply set isenable = 0 where video_id=?",id);
			List<Reply> clist=Reply.dao.find("select id from reply where video_id = ? ",id);
			delchild(clist);
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	
	private void delchild(List<Reply> list){
		for (int i = 0; i < list.size(); i++) {
			String id=list.get(i).get("id").toString();
			List<Reply> clist=Reply.dao.find("select id from reply where parentid = ? ",id);
			if(clist.size()>0){
				Db.update("update reply set isenable = 0 where parentid = ?",id);
				delchild(clist);
			}
		}
	}
	
}
